/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include <list>
#include <vector>
#include <algorithm>

#include "../IProcess.h"

#include "IGameStateView.h"
#include "ICamera.h"


/**
========================================
Manages all rendering at a high level,
the use of this class is if we wanted to
go 3d, we would 'simply' create a new RenderManager to
handle the 3d Rendering.
========================================
*/
class IRender;
class IRenderManager : public IProcess
{
    
public:
    /** Initialize the render manager */
    virtual int Init()=0;
    /** Update the gamestate views */
	virtual void Update(float  dt)=0;
    /** Close off */
	virtual void Shutdown()=0;

    /** Render the scene */
    virtual void Render(float dt)=0;

    /** Set the Backend-Renderer */
    void SetRenderer( IRender* r ) { m_renderer = r; };
    /** Get the Backend */
    IRender* GetRenderer() { return m_renderer; };

    /*---------------------------------------------------------
        Camera Manager - TODO split this into an actual class
    ----------------------------------------------------------*/

    /** Add a camera to the scene */
    void AddCamera( ICamera* c ) {
        camList.push_back(c);
        c->SetID( (int)camList.size()-1 );
    };

    /** Remove a camera from the scene */
    void RemoveCamera( ICamera* c ) {
        // remove camera
        std::vector<ICamera*>::iterator it = std::find( camList.begin(), camList.end(), c);
        camList.erase(it, camList.end() );
    };

    /** Activate a camera */
    void SetCameraStatus( ICamera* c, bool active, bool mainCamera = false ) {
        std::vector<ICamera*>::iterator it;
        for ( it = camList.begin(); it != camList.end(); it++)
        {
            ICamera* cam = (*it);
            if ( cam == c )
            {
                cam->Activate( active );
                if ( mainCamera ) 
                    cam->SetMainCamera();
                continue;
            }  
            if ( mainCamera ) cam->SetSideCamera();
        }
    };

    /** Retrieve a camera */
    ICamera* GetCamera( int i ) {
        return camList[i];
    };

    /** Get the list of cameras */
    std::vector<ICamera*> GetCameraList() { return camList; };

    /*----------------------------------------------------------------
        GameStateView Manager - Manage the views of gamestates
        activate(display) the correct view for the current active
        states.
    -----------------------------------------------------------------*/

    /** Add a state view to the list, sort by z order */
    void AddStateView( IGameStateView* g) {
        type_StateView::iterator it = stateList.begin();
        for (; it != stateList.end(); it++ )
        {
            if ( (*it)->GetZOrder() > g->GetZOrder() )
                break;
        }
        stateList.insert( it , g);
        
    };

    /** Remove a state */
    void RemoveStateView ( IGameStateView* g ) {
        type_StateView::iterator it = stateList.begin();
        for (; it != stateList.end(); it++ )
        {
            if ( (*it) == g )
            {
                stateList.erase( it );
                return;
            }
        }
    };

    /** De/activate a game state view */
    void SetStateViewStatus( int id , bool active ) {
        type_StateView::iterator it = stateList.begin();
        for (; it != stateList.end(); it++ )
        {
            if ( (*it)->GetID() == id )
            {
                (*it)->SetActive( active );
                return;
            }
        }
    };
protected:
    // list of cameras
    std::vector<ICamera* >  camList;
    // gamestate view list
    typedef std::list< IGameStateView* >  type_StateView;
    type_StateView stateList;

    // Rendering Backend
    IRender*    m_renderer;

};

